!======================================================
! A. Gazizov, LNGS, INFN, 04.12.10
! Contains common values to pass to functions
!======================================================
! January 2011, LNGS
!======================================================
! A. Gazizov, LNGS, update on 15.01.11

module comnoosc
   implicit none
!-------------------------------------------------------------------------------------------------
   real(8),parameter:: Omega_m = 0.3d0 & ! cosmology
                     , Lambda  = 0.7d0 & !
					 , ln10    = 2.3025850929940457d0 ! ln(10)
   real(8) :: z_f, z_f1, lnzf1         & ! max redshift of epoch  z_f ans 1+z_f, ln(z_f1) 
            , tmax                     & ! maximum time \tau = ln(z_f1)
            , gam                      & ! \gamma_g
			, evl                      & ! Q*(1+z)^evl 
            , E_0, E_min               & 
			, lgE_max                  & ! E in eV
			, E_max, E_c               &
			, Emax, EE0                &
			, lgE0					   &
			, xmin, xmax, deltax 	     

!   integer, parameter:: npdesp=2, ldy=npdesp  !, nxp=2001
   integer:: nxp, npdesp, ldy
                      
   integer, private:: i, j
   	
   real(8):: r_0
   real(8),dimension(:),allocatable:: xbreak, dlgEeV
   real(8),dimension(:,:),allocatable:: Flux, dlgE3J, y

   integer:: nstep
   real(8):: tol       ! tolerence for DMOLCH 

   character( 2):: chzf
   character( 3):: chgam
   character( 4):: chEmax
   character( 4):: chevl
   character(18):: fname

!-------------------------------------------------------------------------------------------------
                        CONTAINS
!-----------------------------------------------------------------------------

 Subroutine ReadParamNoOsc
   implicit none

   open(1,file='ParamNoOscill.dat',status='old')
      read(1,'(11X,I4  )') nxp
      read(1,'(10X,I3  )') nstep
      read(1,'(10X,d7.1)') tol
      read(1,'(10X,d7.1)') E_0
      read(1,'(10X,d7.1)') E_min
      read(1,'(10X,d6.1)') z_f
	  read(1,'(11X,A2  )') chzf
      read(1,'(10X,d6.1)') gam
	  read(1,'(11X,A3  )') chgam
      read(1,'(10X,d7.2)') lgE_max
	  read(1,'(11X,A4  )') chEmax
      read(1,'(10X,d6.1)') evl
	  read(1,'(11X,A4  )') chevl
   close(1)

   print*, ' File ParamNoOscill.dat is read!'

   fname = 'N'//chzf//chgam//chEmax//chevl//'.dat'
   print*, ' '//fname

   E_max = 1d1**lgE_max

   npdesp = 1
   ldy    = npdesp
   allocate(xbreak(nxp))
   allocate(dlgEeV(nxp))
   allocate(Flux(ldy,nxp))
   allocate(dlgE3J(ldy,nxp))							
   allocate(y(ldy,nxp))

   z_f1  = 1d0 + z_f		! 1 + z_f
   lnzf1 = dlog(z_f1)		! ln(1 + z_f)
   tmax  = lnzf1

!  The range of energies
   r_0 = dlog(E_0)          ! ln(E_0/eV)
   E_c = E_0/1d9            ! in GeV
   Emax = E_max/1d9         ! in GeV 

! Recalculated range of x=ln(E/E_0)
   xmin = dlog(E_min/E_0)   ! ln(E_min/E_0)	 (ln( 0.1) for E_min = 10^{17} eV )
   xmax = dlog(E_max/E_0)   ! ln(E_max/E_0)  (ln(1000) for E_max = 10^{22} eV )

   deltax = xmax - xmin

!  xbreak = ln(E/E_0),  E_0 = 10^18 eV         
   
   do i = 1,nxp
      xbreak(i) = xmin + deltax*dble(i-1)/dble(nxp-1)
	  dlgEeV(i) = (xbreak(i) + r_0)/ln10  ! lg(E/eV)
	  do j = 1,ldy
	     y(j,i)    = 0d0
      enddo
   enddo

 end Subroutine ReadParamNoOsc
!-----------------------------------------------------------------------------

!***************************************************
! Linear interpoalation

   real(8) Function FLin(x,xa,xb,ya,yb)
      implicit none
	  real(8),intent(in):: x, xa, xb, ya, yb

	  if(xa == xb) then
		 print*, ' Stop, xa = xb in FLin !!!'
		 stop
	  else
	     FLin = ya + (yb - ya)/(xb-xa)*(x-xa)
	  endif   
   end Function FLin
!***************************************************	 


end module comnoosc
!==============================================================


